package com.hebeiboruan.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hebeiboruan.common.exception.CommonException;
import com.hebeiboruan.common.exception.CommonResultEnum;
import com.hebeiboruan.common.exception.ServiceResultEnum;
import lombok.Data;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * http消息
 * 此类是Controller的统一返回类
 * 如果要使用这个类那么请使用他静态的resul和data
 * resul用来返回响应状态
 * data用于返回数据
 *
 * @author 一支枯木
 * @date 2023/04/15
 */
@Data
@Component
public class HttpMessage<T> implements Cloneable{
    /**
     * 数据行数
     */
    private Long count;
    /**
     * 响应状态
     */
    private String state;
    /**
     * 返回数据
     */
    private List<T> data=new ArrayList<>();
    /**
     * 异常
     */
    private String exception;

    public void setState(String state) {
        this.state = state;
    }

    /**
     * 增删改接口报错情况
     * @param exception 异常信息
     */
    public HttpMessage(CommonResultEnum exception) {
        this.exception = exception.getResult();
        this.state=exception.getErrorCode();
        this.count=0L;
        this.data=null;
    }

    /**
     * 返回单个对象
     * @param t 单个对象
     *
     */

    public HttpMessage(T t) {
        this.count = 1L;
        this.data.add(t);
        this.exception="";
        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }
    public HttpMessage(String state,String exception){
        this.exception=exception;
        this.state=state;
    }
    public HttpMessage(List<T> t) {
        this.count = (long) t.size();
        this.data=t;
        this.exception="";
        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }
    /**
     *
     * 返回多个对象
     * @param page Page类型
     *
     */
    public HttpMessage(Page<T> page){
        this.data=page.getRecords();
        if(page.getTotal()!=0){
            this.count=  page.getTotal();
        }else {
            this.count= (long) page.getRecords().size();
        }

        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }

    public void setData(List<T> data) {
        this.count = (long) data.size();
        this.data=data;
        this.exception="";
        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }
    public void setData(Page<T> page) {
        this.data=page.getRecords();
        if(page.getTotal()!=0){
            this.count=  page.getTotal();
        }else {
            this.count= (long) page.getRecords().size();
        }

        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }
    public void setData(T t) {
        this.data=new ArrayList<>();
        this.count = 1L;
        this.data.add(t);
        this.exception="";
        this.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
    }

    public  void setException(CommonResultEnum e){
        this.state=e.getErrorCode();
        this.exception=e.getResult();
    }
    public  void setException(String exception){

        this.exception=exception;
    }
    public HttpMessage() {
    }
    private static HttpMessage httpMessage;
    @Override
    protected HttpMessage clone() throws CloneNotSupportedException {
        HttpMessage clone = (HttpMessage) super.clone();
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String dataStr = objectMapper.writeValueAsString(this.data);
            clone.data=objectMapper.readValue(dataStr,List.class);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return clone;
    }

    public static HttpMessage getHttpMessage() {
        if(httpMessage==null){
            httpMessage=new HttpMessage();
        }
        try {
            return httpMessage.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
    public static HttpMessage result(CommonResultEnum commonResultEnum){
        HttpMessage message = getHttpMessage();
        message.setState(commonResultEnum.getErrorCode());
        message.setException(commonResultEnum.getResult());

        return message;
    }

    public static HttpMessage data(Object t) {
        HttpMessage message = getHttpMessage();

        message.count = 1L;
        message.data.add(t);
        message.exception="";
        message.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
        return message;
    }

    public static HttpMessage data(List t) {
        HttpMessage message = getHttpMessage();

        message.count = (long) t.size();
        message.data=t;
        message.exception="";
        message.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
        return message;
    }
    /**
     *
     * 返回多个对象
     * @param page Page类型
     *
     */
    public static HttpMessage data(Page page){
        HttpMessage message = getHttpMessage();

        message.data=page.getRecords();
        if(page.getTotal()!=0){
            message.count=  page.getTotal();
        }else {
            message.count= (long) page.getRecords().size();
        }

        message.state=ServiceResultEnum.SELECT_TRUE.getErrorCode();
        return message;
    }
    public  static void isBlank(Object... objects){
        ServiceResultEnum result=null;
        for (Object o:
             objects) {
            if(o==null){
                result=  ServiceResultEnum.ISNULL;
            }else {
                Class c=o.getClass();
                switch (c.toString()){
                    case "class java.lang.String" :
                        String str=(String) o;
                        if(str.isEmpty()){
                            result= ServiceResultEnum.REQUIRED;
                        }
                    break;
                    case "class java.lang.Integer":
                        Integer integer=(Integer) o;
                        if(integer<=0){
                            result= ServiceResultEnum.ILLEGALITY_NUMBER;
                        }
                        break;
                    case "Double":
                        Double aDouble=(Double) o;
                        if(aDouble<=0){
                            result= ServiceResultEnum.ILLEGALITY_NUMBER;
                        }
                        break;
                    case "Float":
                        Float f=(Float) o;
                        if(f<=0){
                            result= ServiceResultEnum.ILLEGALITY_NUMBER;
                        }
                        break;
                    case "BigDecimal":
                        BigDecimal b=(BigDecimal) o;
                        if(b.intValue()<=0){
                            result= ServiceResultEnum.ILLEGALITY_NUMBER;
                        }
                     break;
                    default :
                        result= ServiceResultEnum.ILLEGALITY_TYPE;
                    break;
                }
            }
        }
        if(result!=null){
            throw new CommonException(result);
        }

    }


}
